#!/bin/sh

usage() {
	cat <<-EOF
		Usage: ss-rules [options]

		Valid options are:

		    -s <server_host>        hostname or ip of shadowsocks remote server
		    -l <local_port>         port number of shadowsocks local server
		    -c <config_file>        config file of shadowsocks
		    -i <ip_list_file>       a file content is bypassed ip list
		    -a <lan_ips>            lan ip of access control, need a prefix to
		                            define access control mode
		    -b <wan_ips>            wan ip of will be bypassed
		    -w <wan_ips>            wan ip of will be forwarded
		    -e <extra_options>      extra options for iptables
		    -o                      apply the rules to the OUTPUT chain
		    -f                      flush the rules
EOF
}

loger() {
	# 1.alert 2.crit 3.err 4.warn 5.notice 6.info 7.debug
	logger -st ss-rules[$$] -p$1 $2
}

ipt="iptables -t nat"

flush_r() {
	local IPT=$(iptables-save -t nat)

	eval $(echo "$IPT" | grep "_SS_SPEC_RULE_" | \
		sed -e 's/^-A/$ipt -D/' -e 's/$/;/')

	for chain in $(echo "$IPT" | awk '/^:SS_SPEC/{print $1}'); do
		$ipt -F ${chain:1} 2>/dev/null && $ipt -X ${chain:1}
	done

	ipset -X ss_spec_wan_ac 2>/dev/null
	return 0
}

iptab_r() {
	iptables-restore -n <<-EOF
		*nat
		:SS_SPEC_WAN_AC - [0:0]
		$(for ip in $WAN_FW_IP; do
			echo "-A SS_SPEC_WAN_AC -d $ip -j SS_SPEC_WAN_FW"; done)
		$(echo -e "$IPLIST" | \
			sed -e "s/^/-A SS_SPEC_WAN_AC -d /" -e "s/$/ -j RETURN/")
		-A SS_SPEC_WAN_AC -j SS_SPEC_WAN_FW
		COMMIT
EOF
	return $?
}

ipset_r() {
	ipset -! -R <<-EOF || return 1
		create ss_spec_wan_ac hash:net
		$(echo -e "$IPLIST" | sed -e "s/^/add ss_spec_wan_ac /")
		$(for ip in $WAN_FW_IP; do echo "add ss_spec_wan_ac $ip nomatch"; done)
EOF
	$ipt -N SS_SPEC_WAN_AC
	$ipt -A SS_SPEC_WAN_AC -m set --match-set ss_spec_wan_ac dst -j RETURN
	$ipt -A SS_SPEC_WAN_AC -j SS_SPEC_WAN_FW
	return $?
}

fw_rule() {
	$ipt -N SS_SPEC_WAN_FW
	$ipt -A SS_SPEC_WAN_FW -p tcp \
		-j REDIRECT --to-ports $LOCAL_PORT 2>/dev/null || {
		LOCAL_IP=$(uci get network.lan.ipaddr 2>/dev/null)
		[ -n "$LOCAL_IP" ] && \
		$ipt -A SS_SPEC_WAN_FW -p tcp \
			-j DNAT --to-destination $LOCAL_IP:$LOCAL_PORT
	} || {
		loger 3 "Can't redirect, please check the iptables."
		exit 1
	}
	command -v ipset >/dev/null
	return $?
}

ac_rule() {
	local S1 S2

	if [ -z "$LAN_AC_IP" -o "${LAN_AC_IP:0:1}" = "b" ]; then
		S1=RETURN
		S2=SS_SPEC_WAN_AC
	else
		if [ "${LAN_AC_IP:0:1}" = "w" ]; then
			S1=SS_SPEC_WAN_AC
			S2=RETURN
		fi
	fi

	if [ -z "$S2" ]; then
		loger 3 "Bad argument \`-a $LAN_AC_IP\`."
		return 2
	fi

	iptables-restore -n <<-EOF || return 1
		*nat
		:SS_SPEC_LAN_AC - [0:0]
		$(for ip in ${LAN_AC_IP:1}; do
			echo "-A SS_SPEC_LAN_AC -s $ip -j $S1"; done)
		-A SS_SPEC_LAN_AC -j $S2
		COMMIT
EOF
	$ipt -A zone_lan_prerouting -p tcp $EXT_ARGS \
		-m comment --comment "_SS_SPEC_RULE_" -j SS_SPEC_LAN_AC

	if [ "$OUTPUT" = 1 ]; then
		$ipt -A OUTPUT -p tcp $EXT_ARGS \
			-m comment --comment "_SS_SPEC_RULE_" -j SS_SPEC_WAN_AC
	fi
	return $?
}

while getopts ":s:l:c:i:e:a:b:w:of" arg; do
	case $arg in
		s)
			SERVER=$OPTARG
			;;
		l)
			LOCAL_PORT=$OPTARG
			;;
		c)
			CONFIG=$OPTARG
			;;
		i)
			IGNORE=$OPTARG
			;;
		e)
			EXT_ARGS=$OPTARG
			;;
		a)
			LAN_AC_IP=$OPTARG
			;;
		b)
			WAN_BP_IP=$(for ip in $OPTARG; do echo $ip; done)
			;;
		w)
			WAN_FW_IP=$OPTARG
			;;
		o)
			OUTPUT=1
			;;
		f)
			flush_r
			exit 0
			;;
	esac
done

if [ -f "$CONFIG" ]; then
	eval $(jshn -r "$(cat $CONFIG)" | awk '{
		if($2~/server\047/){printf("server=%s\n",$3)}
		if($2~/local_port\047/){printf("local_port=%s\n",$3)}
	}')
fi

: ${SERVER:=$server}
: ${LOCAL_PORT:=$local_port}

if [ -z "$SERVER" -o -z "$LOCAL_PORT" ]; then
	usage
	exit 2
fi

SERVER=$(resolveip -t60 $SERVER)

if [ -z "$SERVER" ]; then
	loger 3 "Can't resolve the server hostname."
	exit 1
fi

if [ -f "$IGNORE" ]; then
	IGNORE_IP=$(cat $IGNORE 2>/dev/null)
fi

IPLIST=$(cat <<-EOF | grep -E "^([0-9]{1,3}\.){3}[0-9]{1,3}"
	$SERVER
	0.0.0.0/8
	10.0.0.0/8
	100.64.0.0/10
	127.0.0.0/8
	169.254.0.0/16
	172.16.0.0/12
	192.0.0.0/24
	192.0.2.0/24
	192.88.99.0/24
	192.168.0.0/16
	198.18.0.0/15
	198.51.100.0/24
	203.0.113.0/24
	224.0.0.0/4
	240.0.0.0/4
	255.255.255.255
	$WAN_BP_IP
	$IGNORE_IP
EOF
)

flush_r && fw_rule && ipset_r || iptab_r && ac_rule

exit $?
